/**
*	企业加入
**/
app.controller('gJoinCtrl', ['$scope', '$state', '$stateParams', '$http', '$window', '$cookieStore', 'gConfig', 'utils', 'toaster',
  function($scope, $state, $stateParams, $http, $window, $cookieStore, gConfig, utils, toaster) {

    // 列表等信息
    $scope.pageData = {
      industryList: [], // 行业
      productsMainList: [], // 主营商品
      pruductsList: [],  // 详细商品
      posting: false,
      InvitationCode: '',
      step: 1,
      checkerrortip: false,
      searchData: {
        originalObject: {}
      }
    };

    $scope.gConfig = gConfig;



    // 表单数据
    $scope.formData = {
      industry: null,
      productsMain: null,
      pruducts: null,
      mainBusiness: null,
      codeCertificate: '',
      openID: $cookieStore.get('openID')
    }


    // 获取行业信息
    $scope.getOptioinData = function (id, grade, dataItem, callback) {
      utils.get('GetBusessByID', {
          id: id,
          grade: grade
      }).success(function (rs) {
        $scope.pageData[dataItem] = rs;
        if (typeof callback === 'function') {callback();}
      });
    }

    // 获取初始行业信息
    $scope.getOptioinData(-1, -1, 'industryList', function () {

      var id = $scope.pageData.industryList[0].companyBusinessID;
      $scope.formData.industry = $scope.pageData.industryList[0];
      // 获取主营商品
      $scope.getOptioinData(id, 1, 'productsMainList', function () {
        var _pId = $scope.pageData.productsMainList[0].companyBusinessID;
        $scope.formData.productsMain = $scope.pageData.productsMainList[0];
        $scope.getOptioinData(_pId, 2, 'pruductsList', null);
      })
    });

    // 改变行业
    $scope.changeIndustry = function () {
      // 获取主营商品
      $scope.getOptioinData($scope.formData.industry.companyBusinessID, 1, 'productsMainList', function () {
        var _pId = $scope.pageData.productsMainList[0].companyBusinessID;
        $scope.formData.productsMain = $scope.pageData.productsMainList[0];
        $scope.getOptioinData(_pId, 2, 'pruductsList', null);
      })
    };

    // 改变主营商品
    $scope.changeProductsMain = function () {
      $scope.getOptioinData($scope.formData.productsMain.companyBusinessID, 2, 'pruductsList', null);
    };

    // 后退
    $scope.goBack = function () {
      $window.history.back();
    }

    // 验证企业名称
    $scope.checkCompany = function () {
      if (!angular.element('#invitationCode_value').val().length ) {return false}
      $scope.pageData.posting = true;
      utils.get('checkcode', {
        openID: $cookieStore.get('openID') || '',
        // openID: '122',
        invitationCode: angular.element('#invitationCode_value').val()
      }).success(function (rs) {
        $scope.pageData.posting = false;


        if ( rs.status == 200 ||  rs.status==3) {
          $state.go('g.account');
        } else if (rs.status ==1 || rs.status==2){
          $scope.pageData.searchData.originalObject.name = angular.element('#invitationCode_value').val()
          $scope.pageData.step = 2;
        } else {
          $scope.pageData.checkerrortip;
        }
      })

        //  $scope.pageData.step = 2
        //  $scope.pageData.searchData.originalObject.name = angular.element('#invitationCode_value').val()
    }



    // 提交企业信息
    $scope.postCompanyInfo = function () {
      $scope.pageData.posting = true;
      var _fd = $scope.formData;
      $scope.formData.name = $scope.pageData.searchData.originalObject.name;
      $scope.formData.openID = $cookieStore.get('openID') || '';
      $scope.formData.codeCertificate = $scope.pageData.searchData.originalObject.codeCertificate;
      $scope.formData.address = $scope.pageData.searchData.originalObject.address;
      $scope.formData.enterpriseCustomCode = $scope.pageData.searchData.originalObject.enterpriseCustomCode;
      $scope.formData.email = $scope.pageData.searchData.originalObject.email;
      $scope.formData.legalPerson = $scope.pageData.searchData.originalObject.legalPerson;
      $scope.formData.legalTel = $scope.pageData.searchData.originalObject.legalTel;

      // $scope.formData.openID = '122';


      // 验证是否有必填项没填
      if(!$scope.formData.name.length) {
        toaster.pop({
                type: 'error',
                title: '错误',
                body: '企业名称为必填项',
                timeout: 3000
            });
              $scope.pageData.posting = false;
            return false;
      }

      if(!$scope.formData.codeCertificate.length) {
        toaster.pop({
                type: 'error',
                title: '错误',
                body: '统一社会信用代码为必填项',
                timeout: 3000
            });
              $scope.pageData.posting = false;
            return false;
      }

      utils.post('registeCompany', $scope.formData).success(function (rs) {
        if (rs.status == 200) {
          $state.go('g.account');
        // } else if (rs.status == 1) {
        } else if (rs.status == 1) {
          toaster.pop({
                  type: 'error',
                  title: '错误',
                  body: rs.msg,
                  timeout: 3000
              });
          $scope.pageData.posting = false;
        }
      })
    }
  }
]);
